const tableColumns = [ //表头
    {field: 'id', title: 'ID', type: 'checkbox', fixed: 'left'}
    , {field: 'id', title: 'ID'}
    , {field: 'name', title: '资源名称'}
    , {field: 'code', title: '资源代码'}
    , {field: 'parent', title: '父资源', templet: data => data.parent ? data.parent.name : ''}
    , {field: 'url', title: '资源路径'}
    , {field: 'resourceType', title: '资源类型', templet: data => data.resourceType ? data.resourceType.name : ''}
    , {title: '操作', width: 100, field: 'right', templet: '#action_row'}

]

const name = 'name';

layui.use(['table', 'form', 'layer', 'jquery'], function (table, form, layer, $) {

    //region 渲染表格
    table.render({
        id: 'LAY-TABLE'
        , elem: '#LAY_TABLE'
        , height: 312
        , url: rootPath //数据接口
        , height: 'full-85'
        , page: true //开启分页
        , toolbar: '#toolbar'
        , cols: [
            tableColumns
        ]
    });
    //endregion

    //region 执行查询操作
    form.on('submit(search)', rs => {
        //点击查询按钮时使用表单的数据重载表格数据
        table.reload('LAY-TABLE', {
            where: rs.field
        })
        return false;
    })
    //endregion

    // region 删除单个记录
    table.on("tool(TABLE_ACTION)", e => {
        if (e.event == 'del') {
            layer.confirm(`是否确认删除[${e.data[name]}]${modelName}?`, () => {
                deleteByIds([e.data.id]).then((error, re) => {
                    layer.msg("删除成功", {icon: 6});
                    e.del();
                }).catch((error, rs) => {
                    layer.msg("删除失败", {icon: 5})
                });
            })
        } else {
            layer.open({
                title: `修改${modelName}`,
                type: 2,
                content: `${rootPath}/${e.data.id}`,
                area: ["400px", "450px"]
            })
        }
    })
    //endregion

    // region 头部工具栏操作
    table.on("toolbar(TABLE_ACTION)", e => {
        if (e.event == 'del') {
            let checkStatus = table.checkStatus("LAY-TABLE");
            layer.confirm(`是否删除选中的${checkStatus.data.length}个${modelName}?`, e => {
                deleteByIds(checkStatus.data.map(account => account.id)).then((error, re) => {
                    layer.msg("删除成功", {icon: 6});
                    table.reload('LAY-TABLE', {}, true)
                }).catch((error, rs) => {
                    layer.msg("删除失败", {icon: 5});
                });
            })
        } else if (e.event == 'add') {
            layer.open({
                title: `新增${modelName}`,
                type: 2,
                content: `${rootPath}/add`,
                area: ["400px", "450px"]
            })
        }

    })
    //endregion

    //region 删除记录操作
    function deleteByIds(ids) {
        return new Promise((resolve, reject) => {
            $.ajax({
                url: `${rootPath}`,
                type: 'delete',
                dataType: 'json',
                data: {ids: ids},
                success(rs) {
                    if (rs.code == 0) {
                        resolve(false, rs)
                    } else {
                        reject(new Error("删除失败"), rs)
                    }
                }, error(e) {
                    reject(e)
                }
            })
        })

    }

    //endregion
});